//
//  VideoDetailVew.swift
//  TestConcurreces
//
//  Created by rolodestar on 2025/6/16.
//

import SwiftUI
import SwiftData
import AVKit
import RoSwiftDataForNetVideoDownload
import BilibiliSearch
struct BiliVideoDetailVew: View {
    @EnvironmentObject var actor: VideoActor
    @Environment(\.modelContext) var modelContext: ModelContext
    var video: BilibiliNetVideo
    lazy var avplayer: AVPlayer = AVPlayer(url: video.localUrl == nil ? video.remoteUrl : video.localUrl!)
    var body: some View {
        VStack{
//            Text("remoteUrl: \(video.remoteUrl)")
            if(video.downloadProgress > 0 && video.downloadProgress < 1){
                HStack{
                    Text("\(Int(video.downloadProgress * 100))%")
                    ProgressView(value: video.downloadProgress, total: 1)
                        .frame( height: 20)
                        .tint(.primary)
                }
            }
            if(video.localUrl == nil){
                
                VideoPlayer(player: AVPlayer(url: video.remoteUrl))
                    
                    .toolbar {
                        ToolbarItem {
                            Button("下载"){
                                print("remoteUrl: \(video.remoteUrl)")
//                                print("valid:\(self.modelContext.contains(video))")
                                Task{
                                    let id = video.persistentModelID
                                    
                                    try await actor.startDownload(id: video.persistentModelID)
                                }
                            }
                        }
                    }
                    .toolbar {
                        ToolbarItem {
                            if(video.downloadProgress > 0 && video.downloadProgress < 1){
                                Button("暂停"){
                                    Task(priority: .background ){
                                        await actor.pauseDownload(id: video.persistentModelID)
                                    }
                                }
                            }
                        }
                    }
                    .toolbar {
                        ToolbarItem {
                            if(video.downloadProgress > 0 && video.downloadProgress < 1 && video.resumeData != nil){
                                Button("恢复"){
                                    Task(priority: .background ){
                                        await actor.resumeDownload(id: video.persistentModelID)
                                    }
                                }
                            }
                        }
                    }
                    .toolbar {
                        ToolbarItem {
                           
                                Button("重新下载"){
                                    
                                    Task(priority: .background ){
                                        try await MainActor.run{
                                            video.resumeData = nil
                                            video.downloadProgress = 0.0
                                            try self.modelContext.save()
                                        }
                                        try await actor.startDownload(id: video.persistentModelID)
                                    }
                                }
                            
                        }
                    }
                    
            }else{
                VideoPlayer(player: AVPlayer(url: video.localUrl!))
            }
        }
    }
}

#Preview {
    BiliVideoDetailVew(video:  BilibiliNetVideo.createModel(from: BilibiliSearchItemParsingResult.debguResult))
}
